﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace Istacee.Personal.Desktop.TimeWatcher
{
    /// <summary>
    /// If you explore the source let me say you... I'm sorry...
    /// I'm really sorry for the code quality... really... 
    /// But as this is a little tool for a personal purpose well, I don't really care.
    /// BTW, hope you'll now be able to track the time passed on your tasks more easily now ;)
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private Timer autoSave;
        private Timer statusRefresh;
        private string _message;
        private string _applicationStatus;
        private static MainViewModel _instance;
        private string state;
        
        public static MainViewModel Instance
        {
            get { return _instance ?? (_instance = new MainViewModel()); }
        }

        public ObservableCollection<TrackedTask> Tasks { get; set; }

        public ICommand AddTaskCommand { get; internal set; }
        public ICommand SaveCommand { get; internal set; }
        public ICommand LoadCommand { get; internal set; }

        public string Message
        {
            get { return _message; }
            set
            {
                if (this._message != value)
                {
                    _message = value;
                    RaisePropertyChanged("Message");
                }
            }
        }
        public string ApplicationStatus
        {
            get { return _applicationStatus; }
            set
            {
                if (value != this._applicationStatus)
                {
                    _applicationStatus = value;
                    RaisePropertyChanged("ApplicationStatus");
                }
            }
        }

        public MainViewModel()
        {
            ApplicationStatus = "Application stats - Initialize";
            Tasks = new ObservableCollection<TrackedTask>();
            AddTaskCommand = new RelayCommand(AddTask);
            SaveCommand = new RelayCommand(SaveTask);
            LoadCommand = new RelayCommand(LoadTask);
            autoSave = new Timer();
            autoSave.AutoReset = true;
            autoSave.Interval = 60*1000;
            autoSave.Elapsed += AutoSaveOnElapsed;
            statusRefresh = new Timer();
            statusRefresh.AutoReset = true;
            statusRefresh.Interval = 500;
            statusRefresh.Elapsed += StatusRefreshOnElapsed;
            LoadTask();
            autoSave.Start();
            statusRefresh.Start();
            state = "Ready";
        }

        private void StatusRefreshOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            this.ApplicationStatus = this.GetTaskState();
        }

        private void AutoSaveOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            SaveTask();
        }

        public void SaveTask()
        {
            TaskSerializer.Save(Tasks.ToList());
            this.Message = String.Format("Save done at {0:HH:mm:ss}", DateTime.Now);
        }

        private void LoadTask()
        {
            Tasks.Clear();
            TaskSerializer.Load().ForEach(t => Tasks.Add(t));
            this.Message = String.Format("Load done at {0:HH:mm:ss}", DateTime.Now);
        }

        public void AddTask()
        {
            Tasks.Add(new TrackedTask());
        }

        private string GetTaskState()
        {
            return String.Format("State : {3} | Total : {2} | {0} started | {1} paused", 
                this.Tasks.Count(t => t.Status), 
                this.Tasks.Count(t => !t.Status), 
                this.Tasks.Count,
                state);
        }
    }
}
